﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Collections.Generic;

namespace SES.CMS.Module
{
    public partial class ucHeader : System.Web.UI.UserControl
    {
        List<Category> lstCt = new List<Category>();
        string s = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new cmsCategoryBL().SelectMenu();
            #region comment
            //rpt.DataSource = dt;
            //rpt.DataBind();
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    Label lb = (Label)rpt.Items[i].FindControl("lbl");
            //    if (Session["lang"].ToString() == "VN")
            //    {
            //        if (int.Parse(dt.Rows[i].ItemArray[0].ToString()) == 1)
            //            lb.Text = "<li><a href='/Default.aspx'>" + dt.Rows[i].ItemArray[1].ToString() + "</a></li>";
            //        else if (int.Parse(dt.Rows[i].ItemArray[0].ToString()) == 3)
            //            lb.Text = "<li><a href='/SanPham.aspx'>" + dt.Rows[i].ItemArray[1].ToString() + "</a></li>";
            //        else if (int.Parse(dt.Rows[i].ItemArray[0].ToString()) == 6)
            //            lb.Text = "<li><a href='/Contact.aspx'>" + dt.Rows[i].ItemArray[1].ToString() + "</a></li>";
            //        else
            //        {
            //            lb.Text = "<li><a href='/" + dt.Rows[i].ItemArray[16].ToString() + "/" + dt.Rows[i].ItemArray[0].ToString() + "/" + FriendlyUrl(dt.Rows[i].ItemArray[1].ToString()) + "'>" + dt.Rows[i].ItemArray[1].ToString() + "</a></li>";
            //        }
            //    }
            //    else
            //    {
            //        if (int.Parse(dt.Rows[i].ItemArray[0].ToString()) == 1)
            //            lb.Text = "<li><a href='/Default.aspx'>" + dt.Rows[i].ItemArray[2].ToString() + "</a></li>";
            //        else if (int.Parse(dt.Rows[i].ItemArray[0].ToString()) == 3)
            //            lb.Text = "<li><a href='/SanPham.aspx'>" + dt.Rows[i].ItemArray[2].ToString() + "</a></li>";
            //        else if (int.Parse(dt.Rows[i].ItemArray[0].ToString()) == 6)
            //            lb.Text = "<li><a href='/Contact.aspx'>" + dt.Rows[i].ItemArray[2].ToString() + "</a></li>";
            //        else
            //        {
            //            lb.Text = "<li><a href='/" + dt.Rows[i].ItemArray[16].ToString() + "/" + dt.Rows[i].ItemArray[0].ToString() + "/" + FriendlyUrl(dt.Rows[i].ItemArray[1].ToString()) + "'>" + dt.Rows[i].ItemArray[2].ToString() + "</a></li>";
            //        }
            //    }
            //}
            #endregion comment
            foreach (DataRow dr in new DataView(new cmsCategoryBL().BuildMenu(), "", "OrderID ASC", DataViewRowState.CurrentRows).ToTable().Rows)
            {
                Category objcat = new Category();
                objcat.CategoryID = int.Parse(dr["CategoryID"].ToString());
                objcat.Title = dr["Title"].ToString();
                objcat.eTitle = dr["eTitle"].ToString();
                objcat.ParentID = int.Parse(dr["ParentID"].ToString());
                objcat.OrderID = int.Parse(dr["OrderID"].ToString());
                lstCt.Add(objcat);
            }

            BuildChildMenus(ConvertCategoryListToTree(lstCt), s);
            //string t = s;
            lblCat.Text = s;

        }
        protected void lkTV_Click(object sender, EventArgs e)
        {
            Changelang("VN");
        }

        protected void lkTA_Click(object sender, EventArgs e)
        {
            Changelang("EN");
        }
        private void Changelang(string nameCulture)
        {
            Session["Lang"] = nameCulture;
            Response.Redirect(Request.Url.AbsolutePath.ToString());
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }


        public string GetLinkByType(string id, int sp)
        {
            string url = "";
            int Catid = int.Parse(id);
            if (sp == 1)
            {
                cmsLoaiSanPhamDO obj = new cmsLoaiSanPhamDO();
                obj.LoaiSanPhamID = Catid;
                obj = new cmsLoaiSanPhamBL().Select(obj);
                if (Session["lang"].ToString() == "VN")
                    url = "/SanPham/LoaiSP/" + obj.LoaiSanPhamID + "/" + FriendlyUrl(obj.TenLoaiSanPham);
                else
                    url = "/SanPham/LoaiSP/" + obj.LoaiSanPhamID + "/" + FriendlyUrl(obj.eTenSanPham);
            }
            else if (sp == 0)
            {
                cmsCategoryDO objCat = new cmsCategoryDO();
                objCat.CategoryID = Catid;
                objCat = new cmsCategoryBL().Select(objCat);
                if (objCat.CategoryTypeID == 0)
                    url = "/Default.aspx";
                if (objCat.CategoryTypeID == 1)
                    url = "/About/" + id + "/" + FriendlyURLLang(objCat);
                else if (objCat.CategoryTypeID == 2)
                    url = "/SanPham.aspx";
                else if (objCat.CategoryTypeID == 3)
                    url = "/Category/" + id + "/" + FriendlyURLLang(objCat);
                else if (objCat.CategoryTypeID == 4)
                    url = "/Contact.aspx";
                else if (objCat.CategoryTypeID == 5)
                    url = "/Articles/" + id + "/" + FriendlyURLLang(objCat);
                else if (objCat.CategoryTypeID == 7)
                    url = "/HoiDap.aspx";
            }
            else
            {
                url = "";
            }


            return url;

        }

        private string FriendlyURLLang(cmsCategoryDO objCat)
        {
            if (Session["lang"].ToString() == "VN")
                return FriendlyUrl(objCat.Title);
            else
                return FriendlyUrl(objCat.eTitle);

        }
        private List<Category> ConvertCategoryListToTree(List<Category> flatList)
        {
            List<Category> rootNodes = new List<Category>();
            foreach (Category node in flatList)
            {
                //The parent of this node in the flat list (if there is one).
                Category parent = flatList.Find(i => i.CategoryID == node.ParentID);
                if (parent == null)
                {
                    //Collect the root nodes to return later...
                    rootNodes.Add(node);
                }
                else
                {
                    //Ignore orphans (should never happen, but just in case)...
                    if (!flatList.Exists(i => i.CategoryID == node.CategoryID))
                        continue;

                    //add this node to the child list of its parent.
                    if (parent.ChildCategory == null)
                        parent.ChildCategory = new List<Category>();
                    parent.ChildCategory.Add(node);
                }
            }
            return rootNodes;
        }

        private void BuildChildMenus(List<Category> Categorys, string sub)
        {
            foreach (Category childCategory in Categorys)
            {
                string rwurl = GetLinkByType(childCategory.CategoryID.ToString(), 0);
                if (childCategory.CategoryID.ToString() == "3")
                {
                    if (Session["lang"].ToString() == "VN")
                        s = s + "<li class='dir'><a href='/SanPham.aspx'>Sản Phẩm</a>";
                    else
                        s = s + "<li class='dir'><a href='/SanPham.aspx'>Product</a>";
                    ArrayList arr = new ArrayList();
                    arr = new cmsLoaiSanPhamBL().SelectAll1(0);
                    if (arr.Count > 0)
                    {
                        s = s + "<ul>";
                        for (int i = 0; i < arr.Count; i++)
                        {
                            cmsLoaiSanPhamDO obj = (cmsLoaiSanPhamDO)arr[i];
                            rwurl = GetLinkByType(obj.LoaiSanPhamID.ToString(), 1);
                            if (Session["lang"].ToString() == "VN")
                                s = s + "<li><a href='" + rwurl + "'>" + obj.TenLoaiSanPham + "</a></li>";
                            else
                                s = s + "<li><a href='" + rwurl + "'>" + obj.eTenSanPham + "</a></li>";
                        }
                        s = s + "</ul></li>";
                    }
                    else
                        s = s + "</li>";
                }

                else if (childCategory.CategoryID.ToString() == "5" || childCategory.CategoryID.ToString() == "2" || childCategory.CategoryID.ToString() == "4")
                {
                    //if (Session["lang"].ToString() == "VN")
                    //    s = s + "<li><a href='/Category/5/Tin-Tuc.aspx'>" + new cmsCategoryBL().Select(new cmsCategoryDO{CategoryID=childCategory.CategoryID}).Title + "</a>";
                    //else
                    //    s = s + "<li><a href='/Category/5/News.aspx'>" + new cmsCategoryBL().Select(new cmsCategoryDO{CategoryID=childCategory.CategoryID}).Title + "</a>";
                    if (Session["lang"].ToString() == "VN")
                        s = s + "<li class='dir'><a href='" + GetLinkByType(childCategory.CategoryID.ToString(), 0) + "'>" + childCategory.Title + "</a>";
                    else
                        s = s + "<li class='dir'><a href='" + GetLinkByType(childCategory.CategoryID.ToString(), 0) + "'>" + childCategory.eTitle + "</a>";
                    ArrayList arr = new ArrayList();
                    arr = new cmsCategoryBL().SelectByParentID1(childCategory.CategoryID, 0);
                    if (arr.Count > 0)
                    {
                        s = s + "<ul>";
                        for (int i = 0; i < arr.Count; i++)
                        {
                            cmsCategoryDO obj = (cmsCategoryDO)arr[i];
                            rwurl = GetLinkByType(obj.CategoryID.ToString(), 0);
                            if (Session["lang"].ToString() == "VN")
                                s = s + "<li><a href='" + rwurl + "'>" + obj.Title + "</a></li>";
                            else
                                s = s + "<li><a href='" + rwurl + "'>" + obj.eTitle + "</a></li>";
                        }
                        s = s + "</ul></li>";
                    }
                    else
                        s = s + "</li>";
                }
                else
                {
                    if (Session["lang"].ToString() == "VN")
                        s = s + "<li><a href='" + rwurl + "'>" + childCategory.Title + "</a>";
                    else
                        s = s + "<li><a href='" + rwurl + "'>" + childCategory.eTitle + "</a>";
                    //}
                    if (childCategory.ChildCategory.Count == 0)
                    {
                        s = s + "</li>";
                        continue;
                    }
                    else
                    {
                        s = s + "<ul>";
                        BuildChildMenus(childCategory.ChildCategory, sub);
                        s = s + "</ul></li>";

                    }
                }

            }
        }

        public partial class Category
        {
            public Int32 CategoryID;
            public String Title;
            public String eTitle;
            public Int32 CategoryTypeID;
            public Int32 LoaiSanPhamID;
            public Int32 ParentID;
            public Int32 OrderID;
            public List<Category> ChildCategory = new List<Category>();
        }

    }
}